package com.mentalhealthplatform.global.rbac.dto.normal;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.mentalhealthplatform.global.rbac.model.AccessLevel;
import com.mentalhealthplatform.global.rbac.model.Endpoint;
import com.mentalhealthplatform.global.rbac.model.Permission;
import lombok.Getter;
import lombok.Setter;

import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
public class EndpointDTO {
    @JsonIgnore
    @ExcelProperty("标识符")
    private String identifier;
    @ExcelProperty("ID")
    private Long id;
    @ExcelProperty("显示名称")
    private String displayName;
    @ExcelProperty("路径")
    private String path;
    @ExcelProperty("方法")
    private String method;
    @ExcelProperty("状态")
    private boolean status;
    @ExcelProperty("是否匿名")
    private boolean allowAnonymous;
    @ExcelProperty("描述")
    private String description;
    @ExcelIgnore
    private AccessLevel accessLevel;


    @JsonIgnore
    @ExcelIgnore
    private Set<PermissionDTO> permissions;

    @JsonProperty("permissions")
    public Set<PermissionDTO>  getPermissions() {
        return permissions;
    }


    @JsonProperty("identifier")
    public String getIdentifier() {
        return identifier;
    }


    // 默认构造函数
    public EndpointDTO() { }

    // 构造函数，根据 Endpoint 转换
    public EndpointDTO(Endpoint endpoint) {
        this.setId(endpoint.getId());
        this.setDisplayName(endpoint.getDisplayName());
        this.setPath(endpoint.getPath());
        this.setMethod(endpoint.getMethod());
        this.setStatus(endpoint.isStatus());
        this.setAccessLevel(endpoint.getAccessLevel());
        this.setAllowAnonymous(endpoint.isAllowAnonymous());
        this.setDescription(endpoint.getDescription());

        Set<PermissionDTO> permissionDTOs = new HashSet<>();
        if (endpoint.getPermissions() != null) {
            for (Permission permission : endpoint.getPermissions()) {
                PermissionDTO permissionDTO = new PermissionDTO();
                permissionDTO.setId(permission.getId());
                permissionDTO.setDisplayName(permission.getDisplayName());
                // 其他字段映射
                permissionDTOs.add(permissionDTO);
            }
        }
        this.setPermissions(permissionDTOs);
    }

    // 静态方法：将 Endpoint 转换为 EndpointDTO
    public static EndpointDTO convertToDTO(Endpoint endpoint) {
        if (endpoint == null) {
            return null;
        }

        EndpointDTO endpointDTO = new EndpointDTO();
        endpointDTO.setId(endpoint.getId());
        endpointDTO.setDisplayName(endpoint.getDisplayName());
        endpointDTO.setPath(endpoint.getPath());
        endpointDTO.setMethod(endpoint.getMethod());
        endpointDTO.setIdentifier(endpoint.getIdentifier());
        endpointDTO.setStatus(endpoint.isStatus());
        endpointDTO.setAccessLevel(endpoint.getAccessLevel());
        endpointDTO.setAllowAnonymous(endpoint.isAllowAnonymous());
        endpointDTO.setDescription(endpoint.getDescription());

        Set<PermissionDTO> permissionDTOs = new HashSet<>();
        if (endpoint.getPermissions() != null) {
            for (Permission permission : endpoint.getPermissions()) {
                PermissionDTO permissionDTO = new PermissionDTO();
                permissionDTO.setId(permission.getId());
                permissionDTO.setIdentifier(permission.getIdentifier());
                permissionDTO.setDisplayName(permission.getDisplayName());
                // 其他字段映射
                permissionDTOs.add(permissionDTO);
            }
        }
        endpointDTO.setPermissions(permissionDTOs);

        return endpointDTO;
    }

    // 静态方法：将 EndpointDTO 转换为 Endpoint
    public Endpoint convertToEntity() {
        Endpoint endpoint = new Endpoint();
        endpoint.setId(this.getId());
        endpoint.setDisplayName(this.getDisplayName());
        endpoint.setPath(this.getPath());
        endpoint.setMethod(this.getMethod());
        endpoint.setStatus(this.isStatus());
        endpoint.setAccessLevel(this.getAccessLevel());
        endpoint.setAllowAnonymous(this.isAllowAnonymous());
        endpoint.setDescription(this.getDescription());

        return endpoint;
    }
}

